// Created by WXX on 2022/1/27 9:34
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：4 ms, 在所有 C++ 提交中击败了88.05%的用户
 * 内存消耗：7 MB, 在所有 C++ 提交中击败了49.61%的用户
 */
class Solution {
public:
    int countValidWords(string s) {
        int res = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') continue;
            int j = i;
            while (j < s.size() && s[j] != ' ') j++;
            res += check(s.substr(i, j - i));
            i = j;
        }
        return res;
    }

    bool check(string s) {
        int cnt1 = 0, cnt2 = 0;
        for (int i = 0; i < s.size(); i++) {
            if (isdigit(s[i])) return false;
            else if (s[i] == '-') {
                if (cnt1 == 1) return false;
                cnt1++;
                if (!i || i == s.size() - 1) return false;
                if (!isletter(s[i - 1]) || !isletter(s[i + 1])) return false;
            } else if (s[i] == '!' || s[i] == '.' || s[i] == ',') {
                if (cnt2 == 1) return false;
                cnt2++;
                if (i != s.size() - 1) return false;
            }
        }
        return true;
    }

    bool isletter(char c) {
        return c >= 'a' && c <= 'z';
    }
};

int main() {

    cout << Solution().countValidWords("cat and  dog") << endl;  // 3
    cout << Solution().countValidWords("!this  1-s b8d!") << endl;  // 0
    cout << Solution().countValidWords("alice and  bob are playing stone-game10") << endl;  // 5
    cout << Solution().countValidWords("he bought 2 pencils, 3 erasers, and 1  pencil-sharpener.") << endl;  // 6

    return 0;
}
